package Twice.day05.homework3.dao.impl;

import Twice.day05.homework3.dao.IProductDao;
import Twice.day05.homework3.entity.Product;

import java.util.Arrays;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/28
 * @Version: 1.0
 */
public class ProductDaoImpl implements IProductDao {
    @Override
    public Product[] sort(Product[] products) {
        Product[] pts = Arrays.copyOf(products,products.length);
        for (int i = 0; i < pts.length; i++) {
            for (int j = i+1; j < pts.length; j++) {
                if(pts[i].getPrice()<pts[j].getPrice()){
                    //交换俩个对象.
                    Product temp = pts[i];
                    pts[i] = pts[j];
                    pts[j] = temp;
                }
            }
        }
        return pts;
    }

    @Override
    public Product[] getByPrice(Product[] products, double price) {
        int count = 0;
        for(int i = 0;i<products.length;i++){
            if(products[i].getPrice()>price){
                count++;
            }
        }

        int pos = 0;

        Product[] pts = new Product[count];
        for(int i = 0;i<count;i++){
            if (products[i].getPrice()>price){
                pts[pos++] = products[i];
            }
        }

        return pts;
    }
}
